package m.jclecai.com.view.klpk;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



import java.util.ArrayList;
import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.bean.klpk.bean.PokerBean;
import m.jclecai.com.utils.PreferenceUtil;

/**
 * Created by andy on 2017/6/9.
 */

public  abstract  class PagerRecyclerAdapter extends RecyclerView.Adapter<PagerRecyclerAdapter.ViewHolder> {

    ArrayList<ArrayList<PokerBean>> mListData;
    private String[] mMissData;


    public PagerRecyclerAdapter(ArrayList<ArrayList<PokerBean>> listData) {
        mListData = listData==null?new ArrayList<ArrayList<PokerBean>>():listData;
    }

    public void addData(List<ArrayList<PokerBean>> mdata){
        int total = mListData.size();
        int size = mdata.size();
        mListData.addAll(mdata);
        notifyItemRangeInserted(total,size);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(getItemLayout(), parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ArrayList<PokerBean> pokerBeen = mListData.get(position);
        holder.mPokerGroup.newPokers(pokerBeen, getPokerCategory());

        //根据本地文件判断是否显示遗漏
        boolean showMissing = PreferenceUtil.isShowMissing(holder.mPokerGroup.getContext());
        if(showMissing){
            holder.mPokerGroup.showMissNumberVisiable(true);

            if(position==0){
                holder.mPokerGroup.showYiLouVisiable(true);
            }else{
                holder.mPokerGroup.showYiLouVisiable(false);
            }

        }else{
            holder.mPokerGroup.showMissNumberVisiable(false);
            holder.mPokerGroup.showYiLouVisiable(false);
        }

    }

    public void setMissData(String[] missData){
        //这里我们是设置遗漏；
        mMissData = missData;
        //notifyDataSetChanged();  这里使用这个方法会出现奇怪的问题；
        for (int i = 0; i < missData.length; i++) {
            notifyItemChanged(i);
        }

    }

    /**
     * 获取牌的玩法的类型；
     * @return
     */
    protected abstract int getPokerCategory();

    /**
     * 获取条目的布局；
     * @return
     */
    public abstract int getItemLayout();

    @Override
    public int getItemCount() {
        return mListData.size();
    }


    public interface OnItemCheckedListener{
        /**
         * 这里我们用来监听Item是否被选中了，
         * @param position  选中的item的位置；
         * @param isChecked item是否被选中；
         */
        public void onItemChecked(int position, boolean isChecked);
    }

    public OnItemCheckedListener mOnItemCheckedListener;

    public OnItemCheckedListener getOnItemCheckedListener() {
        return mOnItemCheckedListener;
    }

    /**
     * @return 将Adapter中的数据进行一个返回
     */
    public List<ArrayList<PokerBean>> getData(){
        return mListData;
    }

    public void setOnItemCheckedListener(OnItemCheckedListener onItemCheckedListener) {
        mOnItemCheckedListener = onItemCheckedListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public   PokerGroup mPokerGroup;

        public ViewHolder(View itemView) {
            super(itemView);
            mPokerGroup = ((PokerGroup) itemView.findViewById(R.id.poker_group));
            //将item的点击的事件，给暴露出去，供别人调用；
            mPokerGroup.setOnCheckedStatusChangeListener(new PokerGroup.OnCheckedStatusChangeListener() {
                @Override
                public void onCheckedStatusChange(boolean isChecked) {
                    if (mOnItemCheckedListener != null) {
                        mOnItemCheckedListener.onItemChecked(getLayoutPosition(),isChecked);
                    }
                }
            });

        }
    }
}
